//
//  ST_DesignDAController.swift
//  btvideo
//
//  Created by Jason on 2022/3/4.
//  Copyright © 2022 JarvanZhang. All rights reserved.
//

import UIKit
//import Rswift
import SwiftyJSON

class ST_DesignDAController: UIViewController,ST_VMControllerLightNavBarProduce {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        defer {
            layout_navigation_bar()
        }
        
        view.backgroundColor = .white
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: statusBarHeight+44, left: 0, bottom: 0, right: 0))
        }
        
        binding()
        
    }
    
    func binding(){
        
        TIANSession(requestUrl: PH.checkNewConfineApi).withUnretained(self).subscribe(onNext: { own,json in
            
            switch json{
            case .success(let jsonValue):
                
                own.tain_navigation_bar.title_nav_btn.setTitle(jsonValue["data"]["title"].stringValue, for: .normal)
                
                own.data_array = jsonValue["data"]["list"].arrayValue
                
                own.collectionView.reloadData()
            case .failure(_):break
            }
            
            
        }).disposed(by: rx.disposeBag)
    }
    
    //MARK: - Properties
    
    lazy var tain_navigation_bar:ST_TIANLightNavigationBar = {
        let c = ST_TIANLightNavigationBar()
        c.backgroundColor = .clear
        return c
    }()
    
    
    /// 列表
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.itemSize = CGSize(width: UIScreen.main.bounds.width, height: 60)
        layout.minimumLineSpacing = 0
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: BTSafeAreaBottom, right: 0)
        collectionView.alwaysBounceVertical = true
        collectionView.backgroundColor = .white
        collectionView.register(cellWithClass: ST_DesignDACell.self)
        collectionView.delegate = self
        collectionView.dataSource = self
        return collectionView
    }()
    
    private var data_array = [JSON]()

}

extension ST_DesignDAController:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data_array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        let cell = collectionView.dequeueReusableCell(withClass: ST_DesignDACell.self, for: indexPath)
        
        if let item = data_array[safe: indexPath.row]{
            cell.setTitle(text: item["title"].stringValue)
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        guard let item = data_array[safe: indexPath.row] else{ return }
        
        switch indexPath.row{
        case 0:
            
            guard let url = URL(string: PH.helpPayHtml.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)) else { return  }
            
            let vc = ST_RegistStepHelperController()
            
            vc.url = url
            
            vc.hiddenfeedback = true
            
            GlobalPresent(toController: vc)
            
        case 1:
            
            self.navigationController?.pushViewController(ST_DesignDAWebController(title: item["web_title"].stringValue, link: item["link"].url), animated: true)
        default:break
        }
        
    }
    
}


private class ST_DesignDACell:UICollectionViewCell{
    
    override init(frame: CGRect){
        super.init(frame: frame)
        
        contentView.addSubviews([rightArrowImageView,titleLabel,line])
        
        rightArrowImageView.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(14)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(25)
            make.centerY.equalToSuperview()
            make.right.equalTo(rightArrowImageView.snp.left).offset(-10)
        }
        
        line.snp.makeConstraints { make in
            make.right.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(-25)
            make.height.equalTo(1)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var rightArrowImageView:UIImageView = {
        let v = UIImageView(image:ST_ImageUtils.imageName("right_arrow"))
        return v
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let l = UILabel()
        l.textColor = #colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
        l.font = .systemFont(ofSize: 16, weight: .regular)
        return l
    }()
    
    fileprivate lazy var line:UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xF2F4F7)
        return v
    }()
    
    func setTitle(text:String) {
        titleLabel.text = text
    }
}


